package com.itzhigui.service.impl;

import com.itzhigui.dto.PayInfoDto;
import com.itzhigui.entity.PayInfo;
import com.itzhigui.entity.User;
import com.itzhigui.mapper.PayInfoMapper;
import com.itzhigui.mapper.UserMapper;
import com.itzhigui.service.PayInfoService;
import com.itzhigui.utils.ResponseEnum;
import com.itzhigui.utils.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author java1412
 * @since 2023-11-17 09:50:34
 */
@Service
@Transactional
public class PayInfoServiceImpl implements PayInfoService {

    @Resource
    private PayInfoMapper payInfoMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseUtil findAll() {
        List<PayInfo> payInfoList = payInfoMapper.findAll();
        List<PayInfoDto> payInfoDtoList = new ArrayList<>();
        payInfoList.forEach(payInfo -> {
            // payInfo复制到payInfoDto
            PayInfoDto payInfoDto = new PayInfoDto();
            BeanUtils.copyProperties(payInfo, payInfoDto);
            // 设置用户信息
            Integer userId = payInfo.getUserId();
            User user = userMapper.findById(userId);
            user.setPassword(null);
            user.setSalt(null);
            payInfoDto.setUser(user);
            payInfoDtoList.add(payInfoDto);
        });
        return ResponseUtil.get(ResponseEnum.PAY_INFO_FINDALL_SUCCESS, payInfoDtoList);
    }
}
